﻿using System.Collections.Generic;
using System.Linq;

namespace Wwtfly.BBS.Framework.Common
{
    public interface IPagedList
    {
        int TotalCount { get; set; }
        int TotalPages { get; set; }
        int PageIndex { get; set; }
        int PageSize { get; set; }
        bool IsPreviousPage { get; }
        bool IsNextPage { get; }
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedList<T> : List<T>, IPagedList
    {
        public PagedList(IEnumerable<T> source, int totalRecords, int index, int pageSize)
        {
            TotalCount = totalRecords;
            if (pageSize != 0)
            {
                TotalPages = totalRecords / pageSize;

                if (totalRecords % pageSize > 0)
                    TotalPages++;
            }

            PageSize = pageSize;
            PageIndex = index;
            AddRange(source.ToList());
        }

        #region IPagedList Members

        public int TotalPages { get; set; }
        public int TotalCount { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool IsPreviousPage
        {
            get { return ((PageIndex - 1) > 0); }
        }

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool IsNextPage
        {
            get { return (PageIndex * PageSize) < TotalCount; }
        }


        /// <summary>
        /// 返回一个默认的 PagedList
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static PagedList<Tt> GetDefaultPageList<Tt>()
        {
            return new PagedList<Tt>(new List<Tt>(), 0, 0, 0);
        }

        #endregion
    }

    public static class Pagination
    {
        #region IQueryable<T> extensions

        //public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        //{
        //    return new PagedList<T>(source, index, pageSize);
        //}

        //public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        //{
        //    return new PagedList<T>(source, index, 10);
        //}

        #endregion

        #region IEnumerable<T> extensions

        /// <summary>
        /// 获取 PagedList对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            return new PagedList<T>(source, pageIndex, pageSize, totalCount);
        }

        /// <summary>
        /// 获取 PagedList对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="pageParams"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>(this IEnumerable<T> source, PageParams pageParams, int totalCount)
        {
            return new PagedList<T>(source, totalCount, pageParams.PageIndex, pageParams.PageSize);
        }

        /// <summary>
        /// 获取 PagedList对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="pageParams"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>(this IEnumerable<T> source, PageParams pageParams)
        {
            return new PagedList<T>(source, pageParams.TotalCount, pageParams.PageIndex, pageParams.PageSize);
        }

        #endregion
    }
}
